#include "set_aliyun_link_dialog.h"
#include "ui_set_aliyun_link_dialog.h"
#include<QMessageBox>
#include <QInputDialog>

qint8 set_aliyun_link_dialog::aly_link_mode = 0;

qint8 set_aliyun_link_dialog::getAliyun_link_mode() const
{
    return aliyun_link_mode;
}

QString set_aliyun_link_dialog::getProductKey() const
{
    return ProductKey;
}

QString set_aliyun_link_dialog::getDeviceName() const
{
    return DeviceName;
}

QString set_aliyun_link_dialog::getDeviceSecret() const
{
    return DeviceSecret;
}

set_aliyun_link_dialog::set_aliyun_link_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::set_aliyun_link_dialog)
{
    ui->setupUi(this);
}

set_aliyun_link_dialog::~set_aliyun_link_dialog()
{
    delete ui;
}

/* 确定配置 */
void set_aliyun_link_dialog::on_confirm_btn_clicked()
{
    set_aliyun_link_dialog::accept();
}

/* 云平台连接方式选择 */
void set_aliyun_link_dialog::on_linke_mode_com_currentIndexChanged(int index)
{
    QString dlgTitle;
    QString txtEdit;
    QString defaultInput;
    bool ok = false;

    this->aliyun_link_mode = index;
    aly_link_mode = index;
    /* 以太网方式进行连接 */
    if(index == 0)
    {
        dlgTitle = "以太网连接所需信息输入对话框";
        txtEdit = "每一行依次为client_id,userName,password";
        defaultInput = "";
        QString str = QInputDialog::getMultiLineText(this,dlgTitle,txtEdit,defaultInput,&ok);
        if(str.isEmpty())
            QMessageBox::warning(nullptr,"警告","输入信息空，可能无法正常连接");
    }
    /* 4G-DTU方式进行连接 */
    else if(index == dtu_4G_mode)
    {
        dlgTitle = "4GDTU连接所需信息输入对话框";
        txtEdit = "每一行依次为productKey,deviceName,deviceSecret";
        defaultInput = "";
        QString str = QInputDialog::getMultiLineText(this,dlgTitle,txtEdit,defaultInput,&ok);
        if(ok && !str.isEmpty())
        {
            QStringList strList = str.split("\n");
            this->ProductKey = strList[0];
            this->ProductKey.remove('\n');
            this->DeviceName = strList[1];
            this->DeviceName.remove('\n');
            this->DeviceSecret = strList[2];
            this->DeviceSecret.remove('\n');
        }
        else if(str.isEmpty())
            QMessageBox::warning(nullptr,"警告","输入信息空，可能无法正常连接");
    }
    /* NB-iot方式进行连接 */
    else if(index == nb_iot_mode)
    {

    }
}

qint8 set_aliyun_link_dialog::getAly_link_mode()
{
    return aly_link_mode;
}
